package com.normal.zbase.impl.base

import android.app.Application
import android.content.Intent
import android.content.res.Resources
import android.os.Bundle
import android.os.Process
import com.didi.drouter.api.DRouter
import com.normal.zbase.BuildConfig
import com.normal.zbase.impl.exceptions.CrashHandler
import com.normal.zbase.impl.http.main.ApiConfig
import com.normal.zbase.impl.http.main.ApiConfig.HTTP_TAG
import com.normal.zbase.impl.http.main.ApiConfig.normalHost
import com.normal.zbase.impl.manager.AppManager
import com.normal.zbase.impl.tools.extend.Logs
import okhttp3.Interceptor

abstract class BaseApplication : Application() {
    private var mStartActivityTime: Long = 0

    /**
     * 指定的主机host
     * @return
     */
    abstract val hostUrlList: String

    /**
     * 指定的自定义拦截器
     * @return
     */
    val interceptorList: MutableList<Interceptor>
        get() = ArrayList()

    /**
     * 是否打印Http日志
     * @return
     */
    open val isPrintHttpLog: Boolean
        get() = true

    /**
     * 日志的TAG
     * @return
     */
    open val hTTP_TAG: String?
        get() = HTTP_TAG

    /**
     * 不输出指定的Url路径的日志 不能包含，一般用来过滤心跳日志，比较拉的后端,一般不用
     * @return
     */
    val filterUrlLogList: MutableList<String>
        get() = ArrayList()

    /**
     * 不输出指定的糟乱的日志(内容) 部分相 ，一般用来过滤息屏的socket失败日志，一般不用
     * @return
     */
    val filterLogMsg: MutableList<String>
        get() = ArrayList()

    override fun onCreate() {
        super.onCreate()
        application = this
        initARouter()
        initConfig()
        init()
    }

    private fun init() {
        normalHost = hostUrlList
        ApiConfig.interceptorList = interceptorList
        ApiConfig.isPrintHttpLog = isPrintHttpLog
        HTTP_TAG = hTTP_TAG!!
        ApiConfig.filterUrlLogList = filterUrlLogList
        ApiConfig.filterLogMsg = filterLogMsg
    }

    override fun getResources(): Resources {
        val resources = super.getResources()
        if (resources != null && resources.configuration.fontScale != 1.0f) {
            val configuration = resources.configuration
            configuration.fontScale = 1.0f
            resources.updateConfiguration(configuration, resources.displayMetrics)
        }
        return resources
    }

    protected open fun initConfig() {
        AppManager.mContext = this
        Logs.instance.initLogback()
        if (!BuildConfig.DEBUG) CrashHandler.instance.init() //application 中初始化
    }

    protected fun destroySDK() {}
    override fun onTerminate() {
        super.onTerminate()
        destroySDK()
    }

    override fun startActivity(intent: Intent, options: Bundle?) {
        if (System.currentTimeMillis() - mStartActivityTime > 500) {
            mStartActivityTime = System.currentTimeMillis()
            super.startActivity(intent, options)
        }
    }

    private fun initARouter() {
        DRouter.init(application);
        if (BuildConfig.DEBUG) {   // 这两行必须写在init之前，否则这些配置在init过程中将无效

        }
    }

    fun exitApp() {
        Process.killProcess(Process.myPid())
        System.exit(1)
    }

    companion object {
        var application: BaseApplication? = null
            private set
    }
}